/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-03-26 16:12:11
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-02-21 10:14:51
 * @FilePath: /vue3-admin/src/views/system/MenusManage/useTable.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ref } from 'vue';
import { useMenu } from '@/service/biz/useMenu';
import { confirm, message } from '@/utils/message';
import { usePermission } from '@/service/usePermission';
import { useUserStore } from '@/store';

export const useTable = () => {
    const { tree, loading, getTree, removeMenu } = useMenu();

    const { updatePermission } = usePermission();

    const { userInfo } = useUserStore();

    const getList = () => {
        getTree();
    };

    const reset = () => {
        getTree();
    };

    const editShow = ref(false);
    const editId = ref<number>();
    const addId = ref<number>();

    const handleEdit = (id?: number, aId?: number) => {
        editShow.value = true;
        editId.value = id;
        addId.value = aId;
    };

    const afterUpdate = () => {
        editShow.value = false;

        getList();
        updatePermission();
    };

    const handleDelete = (id: number) => {
        confirm('是否删除?', 'error')
            .then(() => {
                removeMenu(id)?.then((res) => {
                    message('删除成功', 'success');
                    getList();
                });
            })
            .catch(() => {});
    };

    getList();
    return {
        tree,
        loading,
        getList,
        reset,
        handleEdit,
        editShow,
        editId,
        addId,
        afterUpdate,
        handleDelete
    };
};
